<?php
namespace src\logic;
use \classlib\core as C;
class lsitemsg {
	/**
	 * 发送站内信
	 * @param unknown $title
	 * @param unknown $msg
	 * @param unknown $receiveid
	 * @param number $reftype
	 * @param number $refid
	 * @param number $dictid
	 * @param number $senderid
	 */
	public static function send($title, $msg, $receiveid, $reftype=0, $refid=0, $dictid=1, $senderid=0) {
		$data = array('senderid'=>$senderid, 'dictid'=>$dictid, 'title'=>$title, 'reftype'=>$reftype,
			'refid'=>$refid, 'msg'=>$msg, 'reveiverid'=>$receiveid, 'status'=>1, 'stime'=>C\config::$nowtime);
		C\model::load('pro.pl.msg')->new_one($data);
	}
	
	/**
	 * 发送消息推送
	 * @param unknown $userids
	 * @param unknown $title
	 * @param unknown $msg
	 * @param number $msgtype
	 * @param number $studioid
	 * @param number $userid
	 * @param unknown $attachs
	 */
	public static function push($userids, $title, $msg, $msgtype=0, $studioid=0, $userid=0, $attachs=null) {
		$player = array();
		if ($userids) {
			(!is_array($userids)) && $userids = array($userids);
			$where  = array(array('userid', $userids, \classlib\orm::OP_IN));
			$player = C\model::load('pro.log.onesignal')->get_column(0, -1, $where, 'player');
			if (!$player) {
				return;
			}
		}
		$title= strip_tags($title);
		$msg  = strip_tags($msg);
		$conf = C\config::load('onesignal', false);
		$data = array('messageType'=>strval($msgtype), 'studioID'=>strval($studioid), 'userID'=>$userid);
		$args = array('app_id'=>$conf['appid'], 'contents'=>array('en'=>$msg), 'data'=>$data, 'headings'=>array('en'=>$title));
		if (empty($player)) {
			$args['include_player_ids'] = $player;
		} else {
			$args['included_segments']  = array('All');
		}
		if ($attachs && is_array($attachs)) {
			$args = array_merge($args, $attachs);
		}
		$args = json_encode($args);
		$header = array('Content-Type: application/json', 'Authorization: Basic NGEwMGZmMjItY2NkNy0xMWUzLTk5ZDUtMDAwYzI5NDBlNjJj');
		\classlib\request::run($conf['notify'], $args, $header, 'POST');
	}
}